package com.yimi.yinhepda.views.dispatch_module;

import android.os.Bundle;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.ScrollView;
import android.widget.TextView;

import butterknife.Bind;

import com.yimi.yinhepda.R;
import com.yimi.yinhepda.adapter.unload_offline.DispatchChildDetailAdapter;
import com.yimi.yinhepda.entity.bean.CheckEntity;
import com.yimi.yinhepda.entity.bean.DispatchEntity;
import com.yimi.yinhepda.entity.response.DispatchScanResponse;
import com.yimi.yinhepda.utils.IntentUtils;
import com.yimi.yinhepda.base.BaseInitActivity;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * 派送运单扫描信息
 *
 * @author 000432
 * @date 2017/4/23 0023
 */

public class DispatchWayBillDetailActivity extends BaseInitActivity implements View.OnTouchListener {
    /**
     * 标题栏左边
     */
    @Bind(R.id.title)
    TextView mTvTitle;
    /**
     * 标题栏中间
     */
    @Bind(R.id.num)
    TextView mTvNum;
    @Bind(R.id.activity_compare_waybill)
    EditText mEditWayBill;
    @Bind(R.id.activity_compare_listview)
    ListView mListView;
    @Bind(R.id.activity_compare_back)
    Button mBtnBack;
    /**
     * 总件数
     */
    @Bind(R.id.load_compare_zj)
    TextView mTvTotal;
    /**
     * 已扫描件数
     */
    @Bind(R.id.load_compare_scaned)
    TextView mTvScannedNum;
    /**
     * 未扫描件数
     */
    @Bind(R.id.load_compare_not_scaned)
    TextView mTvNotScannedNum;
    /**
     * 实重
     */
    @Bind(R.id.activity_compare_realWeightQty)
    TextView mTvRealWeight;
    /**
     * 计重
     */
    @Bind(R.id.activity_compare_meterageWeightQty)
    TextView mTvComputeWeight;
    /**
     * 体积
     */
    @Bind(R.id.activity_compare_volume)
    TextView mTvVolume;
    @Bind(R.id.dwbda_scrollView)
    ScrollView mScrollView;
    @Bind(R.id.activity_compare_changeProblem)
    Button mChangeProBtn;
    /**
     * 运单号
     */
    private String mCheckWayBillNo;
    private DispatchChildDetailAdapter mAdapter;
    private List<CheckEntity> mCheckList = new ArrayList<>();
    /**
     * 派送任务集合
     */
    private List<DispatchEntity> mDispatchDataList;

    @Override
    public String getPageName() {
        return getString(R.string.page_dispatch_billdetail);
    }

    @Override
    public int getContentViewId() {
        return R.layout.activity_dispatch_detail_child;
    }

    @Override
    public void initView(Bundle savedInstanceState) {
        mChangeProBtn.setVisibility(View.GONE);

        //标题
        mTvTitle.setText(getString(R.string.check_waybillno_title));
        mTvNum.setVisibility(View.GONE);
        //运单号
        mCheckWayBillNo = getIntent().getStringExtra(IntentUtils.BUILD_WAYBILLNO);
        mDispatchDataList = DispatchScanActivity.mDispatchDataList;

        mEditWayBill.setText(mCheckWayBillNo);
    }

    @Override
    public void initEvent() {
        mBtnBack.setOnClickListener(this);
        mListView.setOnTouchListener(this);
        mEditWayBill.setOnTouchListener(this);
        mScrollView.setOnTouchListener(this);
    }

    /**
     * 由运单号，查询子单号，扫描状态等信息
     */
    @Override
    public void initData() {
        if (mDispatchDataList != null) {
            for (int i = 0; i < mDispatchDataList.size(); i++) {
                //当前行的运单信息，包括子单的集合
                DispatchEntity currentItemInfo = mDispatchDataList.get(i);
                String wayBillNo = currentItemInfo.getWaybillNo() + "";

                if (wayBillNo != null && wayBillNo.equals(mCheckWayBillNo)) {
                    List<DispatchScanResponse.DispatchWaybillChild> childInfoList = currentItemInfo.getChildList();
                    if (childInfoList != null) {
                        for (DispatchScanResponse.DispatchWaybillChild childInfo : childInfoList) {
                            CheckEntity checkEntity = new CheckEntity();
                            checkEntity.setChildWayBillNo(childInfo.getChildNo());
                            if (childInfo.getFlag() == 0) {
                                checkEntity.setStatus(0);
                            } else {
                                checkEntity.setStatus(1);
                            }
                            //列表显示
                            mCheckList.add(checkEntity);

                            //总件数，已扫描，未扫描
                            mTvTotal.setText(childInfoList.size() + "");
                            mTvScannedNum.setText(currentItemInfo.getScanCount() + "");
                            mTvNotScannedNum.setText((childInfoList.size() - currentItemInfo.getScanCount()) + "");

                            //实重，计重，体积
                            mTvRealWeight.setText(currentItemInfo.getRealWeightQty() + "kg");
                            mTvComputeWeight.setText(currentItemInfo.getMeterageWeightQty() + "kg");
                            mTvVolume.setText(currentItemInfo.getVolume() + "m³");
                        }
                    }
                    break;
                }
            }
        }

        //comparator接口进行字符串的大小排序
        Collections.sort(mCheckList, new Comparator<CheckEntity>() {
            @Override
            public int compare(CheckEntity t0, CheckEntity t1) {
                return t0.getChildWayBillNo().compareTo(t1.getChildWayBillNo());
            }
        });
        Collections.sort(mCheckList);
        mAdapter = new DispatchChildDetailAdapter(this, mCheckList, null);
        mListView.setAdapter(mAdapter);
    }

    @Override
    public void onClick(View v) {
        super.onClick(v);
        if (v.getId() == R.id.activity_compare_back) {
            activityBackMethod();
        }
    }

    @Override
    public boolean onTouch(View v, MotionEvent event) {
        switch (v.getId()) {
            case R.id.activity_compare_listview:
                mListView.requestDisallowInterceptTouchEvent(true);
                break;
            case R.id.activity_compare_waybill:
                mScrollView.requestDisallowInterceptTouchEvent(false);
            default:
                mScrollView.requestDisallowInterceptTouchEvent(true);
                break;
        }
        return false;
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event) {
        if (event.getKeyCode() == KeyEvent.KEYCODE_BACK) {
            activityBackMethod();
        }
        return false;
    }

    private void activityBackMethod() {
        finish();
    }
}